CSE 130 – Lecture 7 – C Syntax and Variables
Published on: Wed Feb 10 2010
Punctuation in C
- Statements are terminated with a “;”
- Groups of statements are enclosed by {}
- Commas separate function Arguments
- White space is ignored
- Upper case & Lower case are different
Variables
C has 32 keywords. Do not use these 32 keywords as variable names.
Char = “A”. Stores in the memory an integer representing the character in the system
Unsigned Int = An integer variable that does not reserve a piece of memory for +/-
TYPE SIZE HIERARCHY
Char | Short > 2 > Int | Unsigned | Long | Float > 4 > Double > 8
Use size() to get the exact size of a variable
sizeof(intname) → returns # of bytes used.
This is useful for moving from machine to machine
A variable can always be stored in a variable of a larger or equal size.
If you want to go smaller you must cast.
int x = int(3.149)
x = 3 ← NOTICE, C truncates, it does not round
Conversion Examples
float average = 100.0/80.0 = 12.5
float/int = float = 12.5
int/int = int = 12 (lost the decimal point)
When doing floating point math include the .0 for whole numbers!
Operators in C
Arithmetic Operators: + - * / %
Logical Operators: && || ! == !=
&& and
|| or
! not
== Equal check
!= not equal
a + b
+ is the operator
a and b are operands
x++ post increment
++x pre increment